package yxy.game.pm2.active.module.subordinateclass.weeklycard;

import org.joda.time.DateTime;
import yxy.cherry.data.bean.actweekcark;
import yxy.cherry.data.bean.actweekcarkshop;
import yxy.cherry.data.string.GameString;
import yxy.game.pm2.active.ActiveUtil;
import yxy.game.pm2.active.module.DrawStatus;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.active.module.lockorder.LockOrderInfo;
import yxy.game.pm2.active.module.timemail.TimeMail;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.module.mail.MailHandle;
import yxy.game.pm2.module.mail.MailTypeEnum;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 周卡活动 - 支付相关
 * @author Dell
 *
 */
public class WeeklyPay {

	//bean -----------------------------------------------------------------------
	
	private ArrayList<WeeklyCard> wcs = new ArrayList<WeeklyCard>();
	private ArrayList<WeeklyShop> wss = new ArrayList<WeeklyShop>();
	
	public ArrayList<WeeklyCard> getWcs() {
		return wcs;
	}
	public void setWcs(ArrayList<WeeklyCard> wcs) {
		this.wcs = wcs;
	}
	public ArrayList<WeeklyShop> getWss() {
		return wss;
	}
	public void setWss(ArrayList<WeeklyShop> wss) {
		this.wss = wss;
	}
	
	//func ---------------------------------------------------------------------------
	
//	public static final int canget = 1;
//	public static final int hadget = 2;
//	public static final int cannotget = 3;
	
	/** 创建实例 **/
	public static WeeklyPay create(String playercode) {
		WeeklyPay wp = new WeeklyPay();
		
		ArrayList<WeeklyCard> wcs = new ArrayList<WeeklyCard>();
		Map<String, actweekcark> awcmap = ActDataCenter.getAllData(actweekcark.class, playercode);
		for(actweekcark data : awcmap.values()) {
			WeeklyCard wc = new WeeklyCard();
			wc.setId(data.getId());
			wc.setGoodsid(data.getChargeid());
			wcs.add(wc);
		}

		ArrayList<WeeklyShop> wss = new ArrayList<WeeklyShop>();
		Map<String, actweekcarkshop> awcsmap = ActDataCenter.getAllData(actweekcarkshop.class, playercode);
		for(actweekcarkshop data : awcsmap.values()) {
			WeeklyShop ws = new WeeklyShop();
			ws.setId(data.getId());
			ws.setGoodsid(data.getChargeid());
			ws.setLimitnum(data.getLimit());
			wss.add(ws);
		}
		
		wp.setWcs(wcs);
		wp.setWss(wss);
		return wp;
	}
	
	public void update() {
		//核对 周卡 - 周卡 锁单时间
		for(WeeklyCard wc : getWcs()) {
			DateTime locktime = wc.getLock();
			if(locktime == null) {
				continue;
			}
			
			if(DateTime.now().getMillis() - locktime.getMillis() > LockOrderInfo.lockTime()) {
				wc.setLock(null);
			}
		}
		
		//核对 周卡 - 商城 锁单时间
		for(WeeklyShop ws : getWss()) {
			ArrayList<String> removekeys = new ArrayList<String>();
			
			HashMap<String, DateTime> locktimemap = ws.getLock();
			for(String key : locktimemap.keySet()) {
				if(DateTime.now().getMillis() - locktimemap.get(key).getMillis() > LockOrderInfo.lockTime()) {
					removekeys.add(key);
				}
			}
			
			for(String removekey : removekeys) {
				locktimemap.remove(removekey);				
			}
		}
	}
	
	public void refresh() {
		
	}
	
	public void reset(String playerCode) {
		setWcs(new ArrayList<WeeklyCard>());
		setWss(new ArrayList<WeeklyShop>());
		
		Map<String, actweekcark> awcmap = ActDataCenter.getAllData(actweekcark.class, playerCode);
		for(actweekcark data : awcmap.values()) {
			WeeklyCard wc = new WeeklyCard();
			wc.setId(data.getId());
			wc.setGoodsid(data.getChargeid());
			getWcs().add(wc);
		}

		Map<String, actweekcarkshop> awcsmap = ActDataCenter.getAllData(actweekcarkshop.class, playerCode);
		for(actweekcarkshop data : awcsmap.values()) {
			WeeklyShop ws = new WeeklyShop();
			ws.setId(data.getId());
			ws.setGoodsid(data.getChargeid());
			ws.setLimitnum(data.getLimit());
			getWss().add(ws);
		}
	}
	
	public void check() {
		// TODO Auto-generated method stub
		
	}
	
	public int canbuy(int goodsid) {
		for(WeeklyCard wc : getWcs()) {
			if(wc.getGoodsid() == goodsid) {
				if(wc.getBuy() != null) {
					return DrawStatus.hadget.getStatus();
				}
				
				if(wc.getBuy() == null && wc.getLock() == null) {
					return DrawStatus.canget.getStatus();
				}

			}
		}
		
		for(WeeklyShop ws : getWss()) {
			if(ws.getGoodsid() == goodsid) {
				if(ws.getBuy().size() + ws.getLock().size() < ws.getLimitnum()) {
					return DrawStatus.canget.getStatus();
				}
			}
		}
		
		return DrawStatus.cannotget.getStatus();
	}
	
	public void lockorder(int goodsid, String myorderid) {
		for(WeeklyCard wc : getWcs()) {
			if(wc.getGoodsid() == goodsid) {
				wc.setLock(DateTime.now());
			}
		}
		
		for(WeeklyShop ws : getWss()) {
			if(ws.getGoodsid() == goodsid) {
				ws.getLock().put(myorderid, DateTime.now());
			}
		}
		
	}
	
	public void unlockorder(int goodsid, String myorderid) {
		for(WeeklyCard wc : getWcs()) {
			if(wc.getGoodsid() == goodsid) {
				wc.setLock(null);
			}
		}
		
		for(WeeklyShop ws : getWss()) {
			if(ws.getGoodsid() == goodsid) {
				ws.getLock().remove(myorderid);
			}
		}
	}
	
	public void handlebuy(int goodsid, String myorderid, String playercode) {
		for(WeeklyCard wc : getWcs()) {
			if(wc.getGoodsid() == goodsid) {
				wc.setBuy(DateTime.now());
				// TODO 7天奖励邮件
				actweekcark data = ActDataCenter.getData(wc.getId(), actweekcark.class, playercode);
				ArrayList<Currency> curs =  new ArrayList<Currency>();
				curs.add(Currency.create(data.getSignrewardtype(), data.getSignrewardid(), data.getSignrewardnum()));
				
				//发送当天邮件
				String title = GameString.周卡奖励_titl().getText();
				String content = GameString.周卡奖励_msg().getText();
				new MailHandle().sendMail(playercode, MailTypeEnum.Mail, title, content, CCReason.活动_周卡, curs);
				
				try {
					if(TimeMail.lockTimeMail()) {
						DateTime sendtime = ActiveUtil.time0();
						for(int i = 1; i <= 7; i++) {	
							DateTime needsendtime = sendtime.plusDays(i);
							TimeMail.create(needsendtime, playercode, MailTypeEnum.Mail, title, content, CCReason.活动_周卡, curs);
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}finally {
					TimeMail.unlockTimeMail();
				}
				
			}
		}
		
		for(WeeklyShop ws : getWss()) {
			if(ws.getGoodsid() == goodsid) {
				ws.getBuy().put(myorderid, DateTime.now());
			}
		}
	}

}
